<template>
  <el-dialog :title="$t('finance.report.accountMonitoring.edit')" :visible.sync="showState" :before-close="handleClose" :close-on-click-modal="false" width="80%">
    <el-container>
      <el-aside width="300px">
        <el-image
          v-if="formValidate.userImg"
          style="width: 150px; height: 150px"
          :src="formValidate.userImg"
          :preview-src-list="[formValidate.userImg]"
        />
        <p v-if="formValidate.userAccount">{{ $t('financeModule.repaymentNotice.inModal.userAccount') }} : {{ formValidate.userAccount }} </p>
        <el-button @click="toUserAccountMore">{{ $t('financeModule.repaymentNotice.inModal.showUserAccountMore') }}</el-button>
      </el-aside>
      <el-main>
        <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="120px" class="demo-ruleForm">
          <el-form-item :label="$t('financeModule.repaymentNotice.inModal.orderNumber')">
            <el-input
              v-model="formValidate.orderNo"
              readonly
            />
          </el-form-item>
          <el-form-item :label="$t('financeModule.repaymentNotice.inModal.repayMentType')">
            <el-select v-model="formValidate.repayType" readonly>
              <el-option
                v-for="item in $t('financeModule.repaymentNotice.inModal.repayMentTypes')"
                :key="item.value"
                :label="item.desc"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('financeModule.repaymentNotice.inModal.repayMentMoney')">
            <el-input
              v-model="formValidate.repayMoney"
              readonly
            >
              <i slot="suffix">RP</i>
            </el-input>
          </el-form-item>
          <el-form-item :label="$t('financeModule.repaymentNotice.inModal.repayMentProof')">
            <el-image
              v-if="formValidate.repayProof"
              style="width: 150px; height: 150px"
              :src="formValidate.repayProof"
              :preview-src-list="[formValidate.repayProof]"
            />
          </el-form-item>
          <el-form-item :label="$t('financeModule.repaymentNotice.inModal.state')" prop="status">
            <el-select v-model="formValidate.status">
              <el-option
                v-for="item in $t('financeModule.repaymentNotice.inModal.solve')"
                :key="item.value"
                :label="item.desc"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('financeModule.repaymentNotice.inModal.resmark')">
            <el-input
              v-model="formValidate.remark"
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 6}"
              :placeholder="$t('common.input')"
              :maxlength="500"
              show-word-limit
            />
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateRepayProof } from '@/api/finance'
import { ObjToQueryString } from '@/utils/method'
export default {
  name: 'RepaymentNoticeDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      submitLoading: false,
      formValidate: {
        id: null,
        userId: null,
        userImg: null,
        userAccount: null,
        orderNo: null,
        repayType: null,
        repayMoney: null,
        repayProof: null,
        status: null,
        remark: null
      },
      rules: {
        status: [{ required: true, message: this.$t('financeModule.repaymentNotice.inModal.state') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    handleClose() {
      this.$emit('change-dialog', false)
      this.$refs['formValidate'].clearValidate()
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const data = {
            id: this.formValidate.id,
            status: this.formValidate.status,
            remark: this.formValidate.remark
          }
          updateRepayProof(data)
            .then(res => {
              this.submitLoading = false
              if (res.success) {
                this.$message.success(this.$t('common.tip.editSuccess'))
                this.$emit('change-dialog', false)
                this.$emit('edit-success')
                this.handleClose()
              } else {
                this.$message.error(res.message)
              }
            })
            .catch(err => {
              this.$message.error(err.message)
              this.submitLoading = false
            })
        }
      })
    },
    toUserAccountMore() {
      const paramData = {
        from: 'repaymentNotice',
        userId: this.formValidate.userId
      }
      const para = ObjToQueryString(paramData)
      window.open(`${window.location.origin + window.location.pathname}#/usr/detail?${para}`, '_blank')
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select{
  width: 100%;
}
.el-aside{
  text-align: center;
  padding: 20px 5px
}
</style>
